package com.dingpeikeji.quickdeer.modules.wx.driver.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.driver.service.TDriversService;
import com.dingpeikeji.quickdeer.modules.base.web.FSmsController;
import com.dingpeikeji.quickdeer.modules.utils.ValidateCodeTool;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;
import com.dingpeikeji.quickdeer.modules.wx.driver.vo.BindDriver;
import com.dingpeikeji.quickdeer.utils.JedisClient;
import com.dingpeikeji.quickdeer.utils.StringUtil;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("${wxPath}")
public class WxDriverBindController extends BaseController{
	
	@Autowired
	private TDriversService tDriversService;
	@Autowired
	private JedisClient jedisClient;
	
	/**
	 * 司机绑定
	 * @return
	 */
	@RequestMapping(value="bind",method=RequestMethod.GET)
	public String bind(BindDriver driver, Model model){
		model.addAttribute("driver", driver);
		return "wx/modules/driver/bind";
	}
	
	/**
	 * 处理绑定逻辑
	 * @return
	 */  
	@RequestMapping(value="bind",method=RequestMethod.POST)
	public String dobind(BindDriver driver, String validateCode, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request){
		List<String> messages = Lists.newArrayList(); // 校验的信息集合
		String mobileFromRedisCode = jedisClient.get("registercode: "+driver.getPhone());
		if (StringUtils.isEmpty(mobileFromRedisCode)) {
			messages.add("短信验证码超时");
			addMessage(model, messages.toArray(new String[]{}));
			model.addAttribute("driver", driver);
			return "wx/modules/driver/bind";
		}
		if (StringUtils.isEmpty(validateCode)) {
			messages.add("短信验证码为空");
			addMessage(model, messages.toArray(new String[]{}));
			model.addAttribute("driver", driver);
			return "wx/modules/driver/bind?openid="+driver.getOpenid();
		}
		if (!mobileFromRedisCode.equals(validateCode)) {
			messages.add("短信验证码错误");
			addMessage(model, messages.toArray(new String[]{}));
			model.addAttribute("driver", driver);
			return "wx/modules/driver/bind?openid="+driver.getOpenid();
		}
		if(messages.isEmpty()){
			List<String> redirectMsg = Lists.newArrayList(); // 绑定规则校验信息
			// 微信号已经绑定了则不可继续绑定
			TDrivers query = new TDrivers();
			if (StringUtils.isEmpty(driver.getOpenid())) {
				redirectMsg.add("未知错误..");
				addMessage(redirectAttributes, redirectMsg.toArray(new String[]{}));
				return "redirect:/wx/driver/toIndex";
			}
			query.setWechatid(driver.getOpenid());
			List<TDrivers> driverList = tDriversService.findList(query);
			if(driverList != null && !driverList.isEmpty()) {
				redirectMsg.add("该微信号已经被绑定！");
			}
			// 绑定的手机号必须是数据库已经存在的
			query = new TDrivers();
			query.setMobile(driver.getPhone());
			driverList = tDriversService.findList(query);
			if(driverList == null || driverList.isEmpty()) {
				redirectMsg.add("非司机手机号！");
			} else if(!StringUtils.isEmpty(driverList.get(0).getWechatid())){
				// 手机号不可重复绑定
				redirectMsg.add("该手机号已经被绑定！");
			}
			if(!redirectMsg.isEmpty()) {
				addMessage(redirectAttributes, redirectMsg.toArray(new String[]{}));
				return "redirect:"+Global.getWxPath()+"/bind?openid="+driver.getOpenid();
			}
			
			boolean result = tDriversService.bindDriver(driver.getPhone(), driver.getOpenid());
			if(result){
				redirectAttributes.addFlashAttribute("message", "绑定成功！");
				//return "redirect:"+OAuth.OAUTH_URL_DRIVER;
				request.getSession().setAttribute(OAuth.SESSION_DRIVER, driverList.get(0));
				return "redirect:"+OAuth.OAUTH_DRIVER;
			} else {
				messages.add("绑定失败，请重新绑定");
			}
		}
		addMessage(model, messages.toArray(new String[]{}));
		model.addAttribute("driver", driver);
		return "wx/modules/driver/bind";
	}

}
